%This file creates vlag, v1mkt, v2mkt, vmksh, v1sys, v2sys, vsysh, vtipmsh 
%(for the longer panel) that will be merged STATA for the reduced-form regression 


clear
tic


vndlist=1:13;
textFileName1=sprintf('input4createIV_oldyrs.raw'); %the input file of basic info of all hospitals
B=dlmread(textFileName1); %matrix: [newid year prechs adj_vnd hsa sysid bdtot]
B=sortrows(B,[1,2]);


textFileName11=sprintf('choice.csv'); %output file: the choice of vendors by each hospital
textFileName12=sprintf('vlag.csv'); %output file: whether each vendor was chosen in the last period
textFileName13=sprintf('v1mkt.csv'); %output file: whether each vendor is first market-dominant
textFileName14=sprintf('v2mkt.csv'); %output file: whether each vendor is second market-dominant (for tipping behavior)
textFileName15=sprintf('vmksh.csv'); %output file: the market share of each vendor
textFileName16=sprintf('vtipmsh.csv'); %output file: the difference of market share to the first-dominant vendor's share
textFileName17=sprintf('v1sys.csv'); %output file: whether each vendor is first system-dominant
textFileName18=sprintf('v2sys.csv'); %output file: whether each vendor is second system-dominant
textFileName19=sprintf('vsysh.csv'); %output file: the "share" each vendor by system members

textFileName13e=sprintf('exov1mkt.csv'); %output file: (exogenous) whether each vendor is first market-dominant
textFileName14e=sprintf('exov2mkt.csv'); %output file: (exogenous) whether each vendor is second market-dominant (for tipping behavior)
textFileName15e=sprintf('exovmksh.csv'); %output file: (exogenous) the market share of each vendor
textFileName16e=sprintf('exovtipmsh.csv'); %output file: (exogenous) the difference of market share to the first-dominant vendor's share
textFileName17e=sprintf('exov1sys.csv'); %output file: (exogenous) whether each vendor is first system-dominant
textFileName18e=sprintf('exov2sys.csv'); %output file: (exogenous) whether each vendor is second system-dominant
textFileName19e=sprintf('exovsysh.csv'); %output file: (exogenous) the "share" each vendor by system members


chs=zeros(size(B,1),numel(vndlist));
vlag=zeros(size(B,1),numel(vndlist));
v1mkt=zeros(size(B,1),numel(vndlist));
v2mkt=zeros(size(B,1),numel(vndlist));
vmksh=zeros(size(B,1),numel(vndlist));
v1sys=zeros(size(B,1),numel(vndlist));
v2sys=zeros(size(B,1),numel(vndlist));
vsysh=zeros(size(B,1),numel(vndlist));

exov1mkt=zeros(size(B,1),numel(vndlist));
exov2mkt=zeros(size(B,1),numel(vndlist));
exovmksh=zeros(size(B,1),numel(vndlist));
exov1sys=zeros(size(B,1),numel(vndlist));
exov2sys=zeros(size(B,1),numel(vndlist));
exovsysh=zeros(size(B,1),numel(vndlist));

isv1mkt=zeros(size(B,1),1);
isv1sys=zeros(size(B,1),1);
exoisv1mkt=zeros(size(B,1),1);
exoisv1sys=zeros(size(B,1),1);


%endogenous
for i=1:size(B,1)
    vlag(i,:)=ismember(vndlist,B(i,3));
    chs(i,:)=ismember(vndlist,B(i,4));
    %market-related
    ms=B(B(:,5)==B(i,5)&B(:,2)==B(i,2),3);
    ms(ms==1)=[];
    if isempty(ms)==0
        occur=histc(ms,vndlist);
        occur=reshape(occur,1,[]); 
        vmksh(i,:)=occur/sum(occur);
        tmpms=ms;
        tmpms(tmpms==2)=[];
        tmpms(tmpms==13)=[];
        if isempty(tmpms)==0       
           v1mkt(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
           isv1mkt(i,:)=ismember(B(i,4),vndlist(occur==max(occur)));
           occur(occur==max(occur))=0;
           if max(occur)>0
              v2mkt(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
           end  
        end
    end    
   %system-related
   if B(i,6)>0
      sys=B(B(:,6)==B(i,6)&B(:,2)==B(i,2),3);
      sys(sys==1)=[];
      if isempty(sys)==0
         occur=histc(sys,vndlist);
         occur=reshape(occur,1,[]);
         vsysh(i,:)=occur/sum(occur);         
         v1sys(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
         isv1sys(i,:)=ismember(B(i,4),vndlist(occur==max(occur)));
         %v1sys(i,13)=0; 
         occur(occur==max(occur))=0;
         if max(occur)>0
             v2sys(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
         end               
      end
   end   
end
vtipmsh=repmat(max(vmksh,[],2),1,size(vmksh,2))-vmksh;
vlag(:,1)=0;



%exogenous
for i=1:size(B,1)
    %market-related
    ms=B(B(:,5)==B(i,5)&B(:,2)==B(i,2)&B(:,1)~=B(i,1),3);
    ms(ms==1)=[];
    if isempty(ms)==0
        occur=histc(ms,vndlist);
        occur=reshape(occur,1,[]);
        exovmksh(i,:)=occur/sum(occur);
        tmpms=ms;
        tmpms(tmpms==2)=[];
        tmpms(tmpms==13)=[];
        if isempty(tmpms)==0
           occur=histc(tmpms,vndlist);
           occur=reshape(occur,1,[]);
           exov1mkt(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
           exoisv1mkt(i,:)=ismember(B(i,4),vndlist(occur==max(occur)));
           occur(occur==max(occur))=0;
           if max(occur)>0
              exov2mkt(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
           end           
        end        
    end
    
   %system-related
   if B(i,6)>0
      sys=B(B(:,6)==B(i,6)&B(:,2)==B(i,2)&B(:,1)~=B(i,1),3);
      sys(sys==1)=[];
      if isempty(sys)==0
         occur=histc(sys,vndlist);
         occur=reshape(occur,1,[]);
         exovsysh(i,:)=occur/sum(occur);
         exov1sys(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
         exoisv1sys(i,:)=ismember(B(i,4),vndlist(occur==max(occur)));
         occur(occur==max(occur))=0;
         if max(occur)>0
             exov2sys(i,:)=ismember(vndlist,vndlist(occur==max(occur)));
         end               
      end
   end   
end
exovtipmsh=repmat(max(exovmksh,[],2),1,size(exovmksh,2))-exovmksh;


dlmwrite(textFileName11,[chs,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName12,[vlag,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName13,[v1mkt,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName14,[v2mkt,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName15,[vmksh,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName16,[vtipmsh,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName17,[v1sys,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName18,[v2sys,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName19,[vsysh,B(:,1:2)],'delimiter', ',', 'precision', 8)    

dlmwrite(textFileName13e,[exov1mkt,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName14e,[exov2mkt,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName15e,[exovmksh,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName16e,[exovtipmsh,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName17e,[exov1sys,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName18e,[exov2sys,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName19e,[exovsysh,B(:,1:2)],'delimiter', ',', 'precision', 8)    


textFileName23=sprintf('v1mkt_bed.csv'); %output file: whether each vendor is first market-dominant
textFileName24=sprintf('v2mkt_bed.csv'); %output file: whether each vendor is second market-dominant (for tipping behavior)
textFileName25=sprintf('vmksh_bed.csv'); %output file: the market share of each vendor
textFileName26=sprintf('vtipmsh_bed.csv'); %output file: the difference of market share to the first-dominant vendor's share
textFileName27=sprintf('v1sys_bed.csv'); %output file: whether each vendor is first system-dominant
textFileName28=sprintf('v2sys_bed.csv'); %output file: whether each vendor is second system-dominant
textFileName29=sprintf('vsysh_bed.csv'); %output file: the "share" each vendor by system members

textFileName23e=sprintf('exov1mkt_bed.csv'); %output file: (exogenous) whether each vendor is first market-dominant
textFileName24e=sprintf('exov2mkt_bed.csv'); %output file: (exogenous) whether each vendor is second market-dominant (for tipping behavior)
textFileName25e=sprintf('exovmksh_bed.csv'); %output file: (exogenous) the market share of each vendor
textFileName26e=sprintf('exovtipmsh_bed.csv'); %output file: (exogenous) the difference of market share to the first-dominant vendor's share
textFileName27e=sprintf('exov1sys_bed.csv'); %output file: (exogenous) whether each vendor is first system-dominant
textFileName28e=sprintf('exov2sys_bed.csv'); %output file: (exogenous) whether each vendor is second system-dominant
textFileName29e=sprintf('exovsysh_bed.csv'); %output file: (exogenous) the "share" each vendor by system members


v1mkt_bed=zeros(size(B,1),numel(vndlist));
v2mkt_bed=zeros(size(B,1),numel(vndlist));
vmksh_bed=zeros(size(B,1),numel(vndlist));
v1sys_bed=zeros(size(B,1),numel(vndlist));
v2sys_bed=zeros(size(B,1),numel(vndlist));
vsysh_bed=zeros(size(B,1),numel(vndlist));

exov1mkt_bed=zeros(size(B,1),numel(vndlist));
exov2mkt_bed=zeros(size(B,1),numel(vndlist));
exovmksh_bed=zeros(size(B,1),numel(vndlist));
exov1sys_bed=zeros(size(B,1),numel(vndlist));
exov2sys_bed=zeros(size(B,1),numel(vndlist));
exovsysh_bed=zeros(size(B,1),numel(vndlist));

isv1mkt_bed=zeros(size(B,1),1);
isv1sys_bed=zeros(size(B,1),1);
exoisv1mkt_bed=zeros(size(B,1),1);
exoisv1sys_bed=zeros(size(B,1),1);


%BED size adjustment: ENDOgenous
for i=1:size(B,1)
    % MKT-related
    lmkt=B(B(:,5)==B(i,5)&B(:,2)==B(i,2),[end,3]);  %bdtot prechs
    lmkt(lmkt(:,2)==1,:)=[];    
    if isempty(lmkt)==0
       bvsum=zeros(numel(vndlist),1);
       for j=1:size(lmkt,1)
           bvsum(lmkt(j,2))=bvsum(lmkt(j,2))+lmkt(j,1);      
       end
       vmksh_bed(i,:)=bvsum/sum(bvsum);  
       bvsum(2)=0; %excluding self-developed because SD is not market leading
       bvsum(13)=0; %excluding "others" because "others" is not market leading
       if sum(bvsum)>0
          v1mkt_bed(i,:)=ismember(vndlist,vndlist(bvsum==max(bvsum)));
          bvsum(bvsum==max(bvsum))=0;
          if sum(bvsum)>0
             v2mkt_bed(i,:)=ismember(vndlist,vndlist(bvsum==max(bvsum)));             
          end     
       end
    end
    % SYS-related
    if B(i,6)>0
       smkt=B(B(:,6)==B(i,6)&B(:,2)==B(i,2),[end,3]);  %bdtot prechs
       smkt(smkt(:,2)==1,:)=[]; 
       if isempty(smkt)==0
          sysbvsum=zeros(numel(vndlist),1);
          for j=1:size(smkt,1)
              sysbvsum(smkt(j,2))=sysbvsum(smkt(j,2))+smkt(j,1);
          end
          vsysh_bed(i,:)=sysbvsum/sum(sysbvsum);
          if sum(sysbvsum)>0
             v1sys_bed(i,:)=ismember(vndlist,vndlist(sysbvsum==max(sysbvsum)));
          end
          sysbvsum(sysbvsum==max(sysbvsum))=0;
          if sum(sysbvsum)>0
             v2sys_bed(i,:)=ismember(vndlist,vndlist(sysbvsum==max(sysbvsum)));             
          end
       end       
    end
end
vtipmsh_bed=repmat(max(vmksh,[],2),1,size(vmksh,2))-vmksh;




%BED size adjustment: EXOgenous
for i=1:size(B,1)
    % MKT-related
    exolmkt=B(B(:,5)==B(i,5)&B(:,2)==B(i,2)&B(:,1)~=B(i,1),[end,3]);  %bdtot prechs
    exolmkt(exolmkt(:,2)==1,:)=[];    
    if isempty(exolmkt)==0
       exobvsum=zeros(numel(vndlist),1);
       for j=1:size(exolmkt,1)
           exobvsum(exolmkt(j,2))=exobvsum(exolmkt(j,2))+exolmkt(j,1);      
       end
       exovmksh_bed(i,:)=exobvsum/sum(exobvsum);  
       exobvsum(2)=0; %excluding self-developed because SD is not market leading
       exobvsum(13)=0; %excluding "others" because "others" is not market leading
       if sum(exobvsum)>0
          exov1mkt_bed(i,:)=ismember(vndlist,vndlist(exobvsum==max(exobvsum)));
          exobvsum(exobvsum==max(exobvsum))=0;
          if sum(exobvsum)>0
             exov2mkt_bed(i,:)=ismember(vndlist,vndlist(exobvsum==max(exobvsum)));             
          end     
       end
    end
    % SYS-related
    if B(i,6)>0
       exosmkt=B(B(:,6)==B(i,6)&B(:,2)==B(i,2)&B(:,1)~=B(i,1),[end,3]);  %bdtot prechs
       exosmkt(exosmkt(:,2)==1,:)=[]; 
       if isempty(exosmkt)==0
          exosysbvsum=zeros(numel(vndlist),1);
          for j=1:size(exosmkt,1)
              exosysbvsum(exosmkt(j,2))=exosysbvsum(exosmkt(j,2))+exosmkt(j,1);
          end
          exovsysh_bed(i,:)=exosysbvsum/sum(exosysbvsum);
          if sum(exosysbvsum)>0
             exov1sys_bed(i,:)=ismember(vndlist,vndlist(exosysbvsum==max(exosysbvsum)));
          end
          exosysbvsum(exosysbvsum==max(exosysbvsum))=0;
          if sum(exosysbvsum)>0
             exov2sys_bed(i,:)=ismember(vndlist,vndlist(exosysbvsum==max(exosysbvsum)));            
          end
       end       
    end
end
exovtipmsh_bed=repmat(max(exovmksh,[],2),1,size(exovmksh,2))-exovmksh;



dlmwrite(textFileName23,[v1mkt_bed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName24,[v2mkt_bed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName25,[vmksh_bed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName26,[vtipmsh_bed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName27,[v1sys_bed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName28,[v2sys_bed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName29,[vsysh_bed,B(:,1:2)],'delimiter', ',', 'precision', 8)    

dlmwrite(textFileName23e,[exov1mkt_bed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName24e,[exov2mkt_bed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName25e,[exovmksh_bed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName26e,[exovtipmsh_bed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName27e,[exov1sys_bed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName28e,[exov2sys_bed,B(:,1:2)],'delimiter', ',', 'precision', 8)    
dlmwrite(textFileName29e,[exovsysh_bed,B(:,1:2)],'delimiter', ',', 'precision', 8)    












toc



